package com.youlai.boot.system.controller;

import com.youlai.boot.common.annotation.Log;
import com.youlai.boot.common.enums.LogModuleEnum;
import com.youlai.boot.system.mapper.OrgMapper;
import com.youlai.boot.system.mapper.RoleMapper;
import com.youlai.boot.system.mapper.UserMapper;
import com.youlai.boot.system.mapper.UserRoleMapper;
import com.youlai.boot.system.model.vo.OrgDictMap;
import com.youlai.boot.system.model.vo.OrgUserOptionVO;
import com.youlai.boot.system.service.OrgService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.OrgForm;
import com.youlai.boot.system.model.query.OrgQuery;
import com.youlai.boot.system.model.vo.OrgVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.util.List;

/**
 * 机构管理前端控制层
 *
 * @author youlaitech
 * @since 2025-02-22 15:45
 */
@Tag(name = "机构管理接口")
@RestController
@RequestMapping("/api/v1/orgs")
@RequiredArgsConstructor
public class OrgController {

    private final OrgService orgService;

    @Log(value = "机构管理查询", module = LogModuleEnum.ORG)
    @Operation(summary = "机构管理分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('sys:org:query')")
    public PageResult<OrgVO> getOrgPage(OrgQuery queryParams) {
        IPage<OrgVO> result = orgService.getOrgPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增机构管理")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:org:add')")
    public Result<Void> saveOrg(@RequestBody @Valid OrgForm formData) {
        boolean result = orgService.saveOrg(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取机构管理表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('sys:org:edit')")
    public Result<OrgForm> getOrgForm(
            @Parameter(description = "机构管理ID") @PathVariable Long id) {
        OrgForm formData = orgService.getOrgFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改机构管理")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('sys:org:edit')")
    public Result<Void> updateOrg(
            @Parameter(description = "机构管理ID") @PathVariable Long id,
            @RequestBody @Validated OrgForm formData) {
        boolean result = orgService.updateOrg(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除机构管理")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:org:delete')")
    public Result<Void> deleteOrgs(
            @Parameter(description = "机构管理ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        boolean result = orgService.deleteOrgs(ids);
        return Result.judge(result);
    }

    @Operation(summary = "机构字典")
    @GetMapping("/dict")
    public Result<List<OrgDictMap>> getOrgOptions() {
        List<OrgDictMap> orgDictMaps = orgService.list().stream().map(org -> {
            OrgDictMap orgDictMap = new OrgDictMap();
            orgDictMap.setOrgId(org.getId());
            orgDictMap.setOrgName(org.getOrgName());
            return orgDictMap;
        }).toList();
        return Result.success(orgDictMaps);
    }

    /**
     * 根据orgId获取该机构下的所有工作人员
     * 
     * @return
     */
    @GetMapping("/{orgId}/byOrgId")
    public Result<List<OrgUserOptionVO>> getWorkersByOrgId(@PathVariable("orgId") String orgId) {
        return Result.success(orgService.getWorkersByOrgId(orgId));
    }

    /**
     * 根据requestId获取该机构下的所有工作人员
     * 
     * @return
     */
    @GetMapping("/{requestId}/byRequestId")
    public Result<List<OrgUserOptionVO>> getWorkersByRequestId(@PathVariable("requestId") String requestId) {
        return Result.success(orgService.getWorkersByRequestId(requestId));
    }


    @GetMapping("/all-workers")
    public Result<List<OrgUserOptionVO>> getAllWorkerOptions(){
        return Result.success(orgService.getAllWorkerOptions());
    }

}
